## creates tables for appendix D:
## rich state / poor state
## table A6

rm(list=ls())

library(tidyverse)
library(kableExtra)

# read in data

opinion_bootstrapped <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners_richstatepoorstate.RDS')

for (i in names(opinion_bootstrapped)) {
  assign(i, opinion_bootstrapped[[i]])
}

dem_polarization_boot_richstate <- dem_inctop_richstate_boot - dem_incbottom_richstate_boot
rep_polarization_boot_richstate <- rep_inctop_richstate_boot - rep_incbottom_richstate_boot
dem_polarization_boot_poorstate <- dem_inctop_poorstate_boot - dem_incbottom_poorstate_boot
rep_polarization_boot_poorstate <- rep_inctop_poorstate_boot - rep_incbottom_poorstate_boot


# table A6 - rich state poor state

polarization_tab <- data.frame(expand.grid(party = c('rep', 'dem'),
                                           state = c('richstate', 'poorstate'),
                                           topic_6 = unique(issuetopics$topic_6),
                                           stringsAsFactors = FALSE),
                               avgpol = NA,
                               signif = NA,
                               gilens = NA,
                               gilenssignif = NA,
                               disagree = NA,
                               disagreegilens = NA,
                               n_quest = NA,
                               stringsAsFactors = FALSE)

for (i in 1:nrow(polarization_tab)) {
  temp.issues <- issuetopics$question[issuetopics$topic_6 == polarization_tab$topic_6[i]]
  temp.rich <- get(paste0(polarization_tab$party[i], '_inctop_', polarization_tab$state[i], '_boot'))
  temp.rich <- temp.rich[rownames(temp.rich) %in% temp.issues,]
  temp.poor <- get(paste0(polarization_tab$party[i], '_incbottom_', polarization_tab$state[i], '_boot'))
  temp.poor <- temp.poor[rownames(temp.poor) %in% temp.issues,]
  temp.pol <- get(paste0(polarization_tab$party[i], '_polarization_boot_', polarization_tab$state[i]))
  temp.pol <- temp.pol[rownames(temp.pol) %in% temp.issues,]
  
  polarization_tab$avgpol[i] <- median(colMeans(abs(temp.pol)))
  
  temp.ci <- apply(temp.rich - temp.poor, MARGIN = 1, function(x) quantile(x, c(0.025, 0.975)))
  polarization_tab$signif[i] <- sum(!apply(temp.ci, MARGIN = 2, function(x) between(0, x[1], x[2])))
  
  polarization_tab$gilens[i] <- sum(abs(apply(temp.pol, MARGIN = 1, median)) > 0.1)
  
  temp.ci <- apply(temp.pol, MARGIN = 1, function(x) quantile(x, c(0.025, 0.975)))
  polarization_tab$gilenssignif[i] <- sum(apply(temp.ci, MARGIN = 2, function(x) all(x > 0.1) | all(x < -0.1)))
  
  temp.10pt <- apply(temp.ci, MARGIN = 2, function(x) all(x > 0.1) | all(x < -0.1))
  
  temp.disagree <- abs(apply((temp.rich > 0.5) - (temp.poor > 0.5), MARGIN =  1, median))
  
  polarization_tab$disagree[i] <- sum(temp.disagree)
  
  polarization_tab$disagreegilens[i] <- sum(temp.disagree*as.numeric(temp.10pt))
  
  polarization_tab$n_quest[i] <- length(temp.issues)
}

for (p in unique(polarization_tab$party)) {
  for (g in unique(polarization_tab$state)) {
    i <- nrow(polarization_tab) + 1
    polarization_tab[i,] <- NA
    polarization_tab$topic_6[i] <- 'Total'
    polarization_tab$party[i] <- p
    polarization_tab$state[i] <- g
    
    temp.pol <- get(paste0(p, '_polarization_boot_', g))
    
    polarization_tab$avgpol[i] <- median(colMeans(abs(temp.pol)))
    
    for (v in c('signif', 'gilens', 'gilenssignif', 'disagree', 'disagreegilens', 'n_quest')) { 
      polarization_tab[i,v] <- sum(polarization_tab[polarization_tab$party == p & polarization_tab$state == g, v], na.rm = TRUE)
    }
    
  }
  
  
  
}

polarization_tab %>% 
  select(party, state, topic_6, n_quest, avgpol, signif, gilens, disagreegilens) %>% 
  arrange(party, state) %>%
  mutate(party = case_when(party == 'rep' ~ 'Rep.',
                           party == 'dem' ~ 'Dem.'),
         state = case_when(state == 'poorstate' ~ 'Poor state',
                           state == 'richstate' ~ 'Rich state'),
         partystate = paste(party, state, sep = ': '),
         topic_6 = case_when(topic_6 == 'foreignpolicy' ~ 'Foreign policy',
                             topic_6 == 'cultural' ~ 'Cultural',
                             topic_6 == 'immigration' ~ 'Immigration',
                             topic_6 == 'lawenforcement' ~ 'Law enforcement',
                             topic_6 == 'economic' ~ 'Economic',
                             topic_6 == 'socialwelfare' ~ 'Social welfare',
                             TRUE ~ topic_6)) %>% 
  mutate_at(vars(signif:disagreegilens), funs(. / n_quest)) %>% 
  mutate_if(is.numeric, round, 3) %>% 
  mutate_at(vars(signif:disagreegilens), funs(paste0(. * 100, '\\%'))) %>% 
  mutate(partystate = cell_spec(partystate, angle = 90, format = 'latex')) %>% 
  select(partystate, topic_6, n_quest, avgpol, signif, gilens, disagreegilens) %>% 
  kable('latex', booktabs = TRUE, escape = FALSE, linesep = '\\addlinespace',
        col.names = linebreak(c(' ', 'Policy\nDomain', 'Number of\nUnique Issues', 'Average\nOpinion Gap', 'Opinion Difference\nStatistically\nSignificant', 
                                'Opinion Gap\nGreater than\n10 pts.', '10 pt. Gap +\nDisagreement\non Policy'),
                              align = 'c'),
        align = c('l', 'l', rep('c', ncol(.)-2))) %>%
  collapse_rows(1, latex_hline = 'major') %>% 
  row_spec(c(7, 14, 21, 28), bold = TRUE) %>% 
  save_kable(file = 'appendix_tables/detailed_polarization_richstatepoorstate.tex')
